package com.itchu.system.service.impl;

import com.itchu.common.exption.ExceptionEnum;
import com.itchu.common.exption.MusicException;
import com.itchu.system.domain.Singer;
import com.itchu.system.domain.Song;
import com.itchu.system.domain.SongList;
import com.itchu.system.mapper.CollectMapper;
import com.itchu.system.mapper.SingerMapper;
import com.itchu.system.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {
    
    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private SingerMapper singerMapper;

    /**
     * 用户收藏
     * @param userId   用户id
     * @param sid
     * @param lid
     * @return
     */
    @Override
    public void userCollect(Long userId, Long sid, Long lid) {

        collectMapper.insertCollection(userId,sid,lid);
    }

    @Override
    public Integer isCollect(Long userId, Long id, Integer type) {
        Integer count = 0;
        if (type == 0) {
            count = collectMapper.findCollectSong(userId,id);
        }else {
            count = collectMapper.findCollectList(userId,id);
        }
        if (count != 0)
            return 1;
        return 0;
    }

    @Override
    public void cancelCollect(Long userId, Long sid, Long lid, Integer type) {
        if (type.equals(0)) {
            collectMapper.cancelCollectSong(userId,sid);
        }else {
            collectMapper.cancelCollectList(userId,lid);
        }
    }

    @Override
    public List<Song> getCollectSongs(Long userId) {
        if (userId == null) {
            throw new MusicException(ExceptionEnum.MUSIC_NOT_CHILDCATEGORY);
        }

        List<Song> list = collectMapper.getCollectSongs(userId);
        List<Singer> singers = singerMapper.selectAll();
        list.stream().forEach(song -> {
            singers.stream().forEach(singer -> {
                if (song.getSingerId().equals(singer.getId())) {
                    song.setSinger(singer);
                }
            });
        });

        return list;
    }

    @Override
    public List<SongList> getCollectLists(Long userId) {
        if (userId == null) {
            throw new MusicException(ExceptionEnum.MUSIC_NOT_CHILDCATEGORY);
        }

        List<SongList> list = collectMapper.getCollectLists(userId);

        return list;
    }

    @Override
    public Long getAllCollect() {

        Long count = collectMapper.getAllCollect();
        return count;
    }
}
